package com.deprice4u_client;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.*;

public class MakeNewRequestOfProduct extends Activity{
	private EditText etTitle;
	private EditText etDescription;
	private EditText etLowPrice;

	private String strUserID;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_request_of_product);
		
		// Bind control
		etTitle = (EditText)this.findViewById(R.id.etTitle);
		etDescription = (EditText)this.findViewById(R.id.etDescription);
        etLowPrice = (EditText)this.findViewById(R.id.etLowPrice);

        // Get User Info
        SharedPreferences settings = getSharedPreferences(Preference.PREFS_NAME, 0);
        strUserID = settings.getString(Preference.USER_ID, "null");
        
      
		/*
		SharedPreferences settings = getSharedPreferences(
				Preference.PREFS_NAME, 0);
		String userId = settings.getString(Preference.USER_ID, "null");
		String email = settings.getString(Preference.EMAIL, "");
		int deposit = settings.getInt(Preference.DEPOSIT, 0);
		Log.i("SM",userId);
		Log.i("SM",email);
		Log.i("SM",String.valueOf(deposit));
		*/
		
	}
	
	public void onbtnSubmitClick2(View v){
    	// Get data
    	String strTitle = this.etTitle.getText().toString();
    	String strDescription = this.etDescription.getText().toString();
    	String strLowPrice = this.etLowPrice.getText().toString();
    	
    	String key = sendPostData(strTitle, strUserID, strDescription, strLowPrice);
    	
    	
    	// TODO Need to modify
    	Toast t = Toast.makeText(this, key, Toast.LENGTH_SHORT);
    	t.show();
    }
	
	private String sendPostData(String strTitle, 
							String strUserID, 
							String strDescription, 
							String strLowPrice){
		String strKey = null;
		URL queryURL=null; 
		StringBuilder strQuery = new StringBuilder("http://socurator.appspot.com/gp");
		strQuery.append("?action=makenew&");
		strQuery.append("title=").append(strTitle).append("&");
		strQuery.append("trigger_userid=").append(strUserID).append("&");
		strQuery.append("description=").append(strDescription).append("&");
		strQuery.append("lprice=").append(strLowPrice);
		
		Log.i("URL", strQuery.toString());
		
		try {
			queryURL = new URL(strQuery.toString());
			
			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			Document doc = docBuilder.parse(queryURL.openStream());
			
			strKey = doc.getElementsByTagName("gp_key").item(0).getChildNodes().item(0).getNodeValue().trim();
			
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return strKey;
	}
	
}
 